module.exports = Behavior({
  data: {
    privacyShow: false,
    privacyContractName: '', //隐私保护协议的名称
    canUseGetPrivacySetting: false, // 低版本基础库（< 2.32.3 ）不支持 wx.getPrivacySetting 接口，隐私接口可以直接调用,
    needAuthorization: false, //隐私协议是否需要授权
  },
  
  pageLifetimes: {
    show(){
      // 获取用户隐私协议设置
      if (wx.getPrivacySetting) {
        wx.getPrivacySetting({
          success: res => {
            if (res.errMsg == "getPrivacySetting:ok") {
            console.log("是否需要授权：", res.needAuthorization, "隐私协议的名称为：", res.privacyContractName)
              this.setData({
                canUseGetPrivacySetting: true,
                privacyContractName: res.privacyContractName ? '《隐私保护协议》' : '',
                needAuthorization: res.needAuthorization
              })
            }
          }
        })
      } else {
        // 低版本基础库不支持 wx.getPrivacySetting 接口，隐私接口可以直接调用
      }
    }
  },

  methods: {
    // 打开用户隐私隐私保护指引
    openPrivacyContract() {
      if (this.data.canUseGetPrivacySetting) {
        wx.openPrivacyContract({
          fail: () => {
            wx.showToast({
              title: '遇到错误',
              icon: 'error'
            })
          }
        })
      }else{
        // wx.navigateTo({
        //   url: '/agreement/pages/user-privacy/index',
        // })
      }
    },

    // 同意隐私保护指引
    handleAgreePrivacyAuthorization(){
      console.log('同意隐私保护指引授权');
      this.setData({
        needAuthorization: false,
        privacyShow: false
      })
    },

      /**
     * 关闭协议弹窗
     */
    onClose(){
      this.setData({
        privacyShow: false
      })
    },
  }
})